package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.group.domain.MetaverseGroup;
import com.ruoyi.group.service.IMetaverseGroupService;
import com.ruoyi.shop.domain.MetaverseShop;
import com.ruoyi.shop.service.IMetaverseShopService;
import com.ruoyi.shopManager.domain.MetaverseShopManager;
import com.ruoyi.shopManager.service.IMetaverseShopManagerService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.users.domain.MetaverseUsers;
import com.ruoyi.users.service.IMetaverseUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/metaverse-users/users")
public class MetaverseUsersController extends BaseController
{
    @Autowired
    private IMetaverseUsersService metaverseUsersService;
    @Autowired
    private IMetaverseShopManagerService shopManagerService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IMetaverseGroupService groupService;
    @Autowired
    private IMetaverseShopService shopService;


    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:list')")
    @GetMapping("/list")
    public TableDataInfo list(MetaverseUsers metaverseUsers)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        System.out.println("user = " + user);
        Long shopManagerId = user.getHostId();
        if (user.getDeptId()==202){
            MetaverseShopManager shopManager = shopManagerService.selectMetaverseShopManagerByShopManagerId(shopManagerId);
            if (shopManager != null){
                Query query = Query.query(Criteria.where("shopId").is(shopManager.getShopId()).and("status").is(1));
                List<MetaverseUsers> usersList = mongoTemplate.find(query,MetaverseUsers.class,"shop_users");
                if (!usersList.isEmpty()){
                    //先设一个空的userId的集合
                    List<String> userId = new ArrayList<>();
                    for (MetaverseUsers users: usersList){
                        //循环遍历出list中的userId
                        String id = users.getUserId();
                        //然后赋值给上面空的userId的集合
                        userId.add(id);
                    }
                    startPage();
                    List<MetaverseUsers> list = metaverseUsersService.selectMetaverseUsersListAop(userId);
                    return getDataTable(list);
                }else {
                    return null;
                }
            }
        }else if (user.getDeptId() == 101){
            MetaverseGroup group = groupService.selectMetaverseGroupByGroupId(shopManagerId);
            if (group != null){
                List<MetaverseShop> shopList = shopService.selectMetaverseShopListAop(group.getGroupId());
                if (shopList != null){
                    List<Long> shopId = new ArrayList<>();
                    for (MetaverseShop shop: shopList){
                        long sid = shop.getShopId();
                        shopId.add(sid);
                    }
                    Query query = Query.query(Criteria.where("shopId").in(shopId).and("status").is(1));
                    List<MetaverseUsers> usersList = mongoTemplate.find(query,MetaverseUsers.class,"shop_users");

                    if (!usersList.isEmpty()){
                        //先设一个空的userId的集合
                        List<String> userId = new ArrayList<>();
//                        if (!userId.isEmpty()){
                            for (MetaverseUsers users: usersList){
                                //循环遍历出list中的userId
                                String id = users.getUserId();
                                //然后赋值给上面空的userId的集合
                                userId.add(id);
                            }
//                        }
                        startPage();
                        List<MetaverseUsers> list = metaverseUsersService.selectMetaverseUsersListAop(userId);
                        return getDataTable(list);

                    }else {
                        return null;
                     }
                }
            }
        }
        startPage();
        List<MetaverseUsers> list = metaverseUsersService.selectMetaverseUsersList(metaverseUsers);
        return getDataTable(list);

    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MetaverseUsers metaverseUsers)
    {
        List<MetaverseUsers> list = metaverseUsersService.selectMetaverseUsersList(metaverseUsers);
        ExcelUtil<MetaverseUsers> util = new ExcelUtil<MetaverseUsers>(MetaverseUsers.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(metaverseUsersService.selectMetaverseUsersById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MetaverseUsers metaverseUsers)
    {
        return toAjax(metaverseUsersService.insertMetaverseUsers(metaverseUsers));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MetaverseUsers metaverseUsers)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        metaverseUsers.setUpdateBy(user.getUserName());
        return toAjax(metaverseUsersService.updateMetaverseUsers(metaverseUsers));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('metaverse-users:users:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(metaverseUsersService.deleteMetaverseUsersByIds(ids));
    }
}
